<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="宠物名称" prop="petName">
        <el-input v-model="queryParams.petName" placeholder="请输入宠物名称" clearable size="small" @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="申请人" prop="userName">
        <el-input v-model="queryParams.userName" placeholder="请输入申请人" clearable size="small" @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="申请状态" clearable size="small">
          <el-option v-for="dict in statusOptions" :key="dict.value" :label="dict.label" :value="dict.value"/>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-table v-loading="loading" :data="adoptionList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="申请编号" align="center" prop="adoptionId"/>
      <el-table-column label="宠物名称" align="center" prop="petName"/>
      <el-table-column label="申请人" align="center" prop="userName"/>
      <el-table-column label="联系电话" align="center" prop="phone"/>
      <el-table-column label="申请时间" align="center" prop="applyTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.applyTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center" prop="status">
        <template slot-scope="scope">
          <span>{{ getStatusLabel(scope.row.status) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-view" @click="handleView(scope.row)"
            v-hasPermi="['admin:adoption:query']">查看</el-button>
          <el-button size="mini" type="text" icon="el-icon-check" @click="handleApprove(scope.row)"
            v-if="scope.row.status === '0'" v-hasPermi="['admin:adoption:approve']">审批</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList"/>

    <!-- 查看对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" label-width="80px">
        <el-form-item label="宠物名称">
          <span>{{ form.petName }}</span>
        </el-form-item>
        <el-form-item label="申请人">
          <span>{{ form.userName }}</span>
        </el-form-item>
        <el-form-item label="联系电话">
          <span>{{ form.phone }}</span>
        </el-form-item>
        <el-form-item label="申请理由">
          <span>{{ form.reason }}</span>
        </el-form-item>
        <el-form-item label="居住地址">
          <span>{{ form.address }}</span>
        </el-form-item>
        <el-form-item label="申请时间">
          <span>{{ parseTime(form.applyTime) }}</span>
        </el-form-item>
        <el-form-item label="状态">
          {{ getStatusLabel(form.status) }}
        </el-form-item>
        <el-form-item v-if="form.status !== '0'" label="审批意见">
          <span>{{ form.comment }}</span>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer" v-if="form.status === '0'">
        <el-button type="primary" @click="handleApprove(form)">审批</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 审批对话框 -->
    <el-dialog title="审批" :visible.sync="approveOpen" width="500px" append-to-body>
      <el-form ref="approveForm" :model="approveForm" :rules="approveRules" label-width="80px">
        <el-form-item label="审批结果" prop="status">
          <el-radio-group v-model="approveForm.status">
            <el-radio label="1">通过</el-radio>
            <el-radio label="2">拒绝</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="审批意见" prop="comment">
          <el-input v-model="approveForm.comment" type="textarea" placeholder="请输入审批意见"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitApprove">确 定</el-button>
        <el-button @click="approveOpen = false">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listAdoption, getAdoption, approveAdoption } from "@/api/admin/adoption";

export default {
  name: "Adoption",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 领养申请表格数据
      adoptionList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 是否显示审批弹出层
      approveOpen: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        petName: null,
        userName: null,
        status: null
      },
      // 表单参数
      form: {},
      // 审批表单参数
      approveForm: {
        adoptionId: null,
        status: '1',
        comment: null
      },
      // 审批表单校验
      approveRules: {
        status: [
          { required: true, message: "请选择审批结果", trigger: "change" }
        ],
        comment: [
          { required: true, message: "请输入审批意见", trigger: "blur" }
        ]
      },
      statusOptions: [
        { value: "0", label: "待审核" },
        { value: "1", label: "已通过" },
        { value: "2", label: "已拒绝" }
      ]
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询领养申请列表 */
    getList() {
      this.loading = true;
      listAdoption(this.queryParams).then(response => {
        this.adoptionList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {};
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 查看按钮操作 */
    handleView(row) {
      this.reset();
      getAdoption(row.adoptionId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "查看领养申请";
      });
    },
    /** 审批按钮操作 */
    handleApprove(row) {
      this.approveForm = {
        adoptionId: row.adoptionId,
        status: '1',
        comment: null
      };
      this.approveOpen = true;
    },
    /** 提交审批 */
    submitApprove() {
      this.$refs["approveForm"].validate(valid => {
        if (valid) {
          approveAdoption(this.approveForm).then(response => {
            this.$modal.msgSuccess("审批成功");
            this.approveOpen = false;
            this.getList();
          });
        }
      });
    },
    /** 多选框选中数据 */
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.adoptionId);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    getStatusLabel(status) {
      const found = this.statusOptions.find(option => option.value === status);
      return found ? found.label : '';
    }
  }
};
</script>
